package com.fh.rolepermission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fh.adminrole.entity.UmsAdminRole;
import com.fh.rolepermission.entity.UmsRolePermission;
import com.fh.rolepermission.mapper.UmsRolePermissionMapper;
import com.fh.rolepermission.service.IUmsRolePermissionService;
import com.fh.search.AdminRoleBO;
import com.fh.search.RolePermissionBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author F
 * @since 2021-05-14
 */
@Service
public class UmsRolePermissionServiceImpl extends ServiceImpl<UmsRolePermissionMapper, UmsRolePermission> implements IUmsRolePermissionService {


    @Override
    public void saveRoleToAdmin(RolePermissionBo rolePermissionBo) {
        //删除用户关联的角色信息
        QueryWrapper<UmsRolePermission> queryWrapper = new QueryWrapper<UmsRolePermission>();
        queryWrapper.eq("ums_role_id", rolePermissionBo.getRoleId());
        remove(queryWrapper);
        if (!CollectionUtils.isEmpty(rolePermissionBo.getPermissionList())) {
            List<UmsRolePermission> adminRoleList = new ArrayList<>();
            rolePermissionBo.getPermissionList().forEach(roleId -> {
                UmsRolePermission umsAdminRole = new UmsRolePermission();
                umsAdminRole.setRoleId(rolePermissionBo.getRoleId());
                umsAdminRole.setUmsRoleId(roleId);
                adminRoleList.add(umsAdminRole);
            });
            saveBatch(adminRoleList);
        }
    }
}
